/********************************** 
/ Replication code  
/ "Social Trust in Polarized Times: 
   How Perceptions of Political Polarization 
   Affect Americans' Trust in Each Other" 
/ Amber Hye-Yon Lee 
/ March 3 2022 
**********************************/ 

use "PerPolarization_SocialTrust_PanelSurvey.dta", clear


xtset CaseId wave
global controls "persecon_w attextremeindx_w extrthermtrump_w pidstren_w ideostren_w disownsex_w disownrace_w"


/* Table 1 */
eststo base: xtreg soctrustindx_w c.perpindx_w i.wave [pweight=WEIGHTA_W5], fe
eststo full: xtreg soctrustindx_w c.perpindx_w i.wave $controls [pweight=WEIGHTA_W5], fe
esttab using model.rtf, b(%8.3fc) se  replace

* calculating effect size using the range of within-person change 
xtreg perpindx_w i.wave [pweight=WEIGHTA], fe
predict resid, r
egen sdresid = sd(resid) 

egen perpolmin = min(perpindx_w), by(CaseId)
egen perpolmax = max(perpindx_w), by(CaseId)
gen withinrange=perpolmax-perpolmin
tab withinrange
di 120/6680 	//1.8% no within-person variation 
sum withinrange	//mean within-person change = 0.28
egen mwithinrange = mean(withinrange)

estimates table base
matrix b=r(coef)
estimates table full
matrix f=r(coef)

di b[1,1] * mwithinrange 
di f[1,1] * mwithinrange 
di b[1,1] * sdresid
di f[1,1] * sdresid

eststo clear


/* Tables in Appendix */ 

* Table A2. Unweighted results 
eststo base: xtreg soctrustindx_w c.perpindx_w i.wave, fe
eststo full: xtreg soctrustindx_w c.perpindx_w i.wave $controls, fe
esttab using unweighted.rtf, b(%8.3fc) se  replace
eststo clear

* Table A3. With party feeling thermometers 
local party "persecon_w attextremeindx_w thermpartydiff_w pidstren_w ideostren_w disownsex_w disownrace_w"
eststo weight: xtreg soctrustindx_w c.perpindx_w i.wave `party' [pweight=WEIGHTA], fe
eststo noweight: xtreg soctrustindx_w c.perpindx_w i.wave `party', fe
esttab using party.rtf, b(%8.3fc) se  replace
eststo clear

* Table A4. Perceived vs affective polarization 
eststo noap: xtreg soctrustindx_w i.wave c.perpindx_w persecon_w disownsex_w disownrace_w [pweight=WEIGHTA], fe
eststo nopp: xtreg soctrustindx_w i.wave $controls [pweight=WEIGHTA], fe
eststo full: xtreg soctrustindx_w c.perpindx_w i.wave $controls [pweight=WEIGHTA], fe
esttab using ppap.rtf, b(%8.3fc) se  replace
eststo clear

* Table A5. ML-SEM
xtdpdml soctrustindx_w, pre(L.perpindx_w) vce(robust)
xtdpdml soctrustindx_w, pre(perpindx_w L.perpindx_w) vce(robust)



**************************
/* Coding of Variables	*/
**************************

* social trust index 
recode ST1_W* (1=1) (2=0) (else=.), gen (strust1_w1 strust1_w2 strust1_w3 strust1_w4 strust1_w5) 
recode ST2_W* (1=0) (2=1) (else=.), gen (strust2_w1 strust2_w2 strust2_w3 strust2_w4 strust2_w5) 
recode ST3_W* (1=1) (2=0) (else=.), gen (strust3_w1 strust3_w2 strust3_w3 strust3_w4 strust3_w5) 
egen soctrustindx_w1=anycount(strust1_w1 strust2_w1 strust3_w1), v(1)
egen soctrustindx_w2=anycount(strust1_w2 strust2_w2 strust3_w2), v(1)
egen soctrustindx_w3=anycount(strust1_w3 strust2_w3 strust3_w3), v(1)
egen soctrustindx_w4=anycount(strust1_w4 strust2_w4 strust3_w4), v(1)
egen soctrustindx_w5=anycount(strust1_w5 strust2_w5 strust3_w5), v(1)
sum soctrustindx_w*

* perceived polarization index 
recode PP1_W* (1=1) (2=2) (3=3) (4=4) (else=.), gen(percp1_w1 percp1_w2 percp1_w3 percp1_w4 percp1_w5)
recode PP2_W* (1=1) (2=2) (3=3) (4=4) (else=.), gen(percp2_w1 percp2_w2 percp2_w3 percp2_w4 percp2_w5)
recode PP3_W* (1=4) (2=3) (3=2) (4=1) (else=.), gen(percp3_w1 percp3_w2 percp3_w3 percp3_w4 percp3_w5)
recode PP4_W* (1=4) (2=3) (3=2) (4=1) (else=.), gen(percp4_w1 percp4_w2 percp4_w3 percp4_w4 percp4_w5)
egen perpindx_w1=rowmean(percp1_w1 percp2_w1 percp3_w1 percp4_w1)
egen perpindx_w2=rowmean(percp1_w2 percp2_w2 percp3_w2 percp4_w2)
egen perpindx_w3=rowmean(percp1_w3 percp2_w3 percp3_w3 percp4_w3)
egen perpindx_w4=rowmean(percp1_w4 percp2_w4 percp3_w4 percp4_w4)
egen perpindx_w5=rowmean(percp1_w5 percp2_w5 percp3_w5 percp4_w5)
sum perpindx_w* 

* personal economic situation
recode ECO3_W* (1=1) (2=2) (5=3) (3=4) (4=5) (else=3), gen(persecon_w1 persecon_w2 persecon_w3 persecon_w4 persecon_w5)
sum persecon_w* 

* attitude extremity: immigration 
recode IM1_W* (1=5) (5=1) (4=2) (2=4) (3=3) (77 98 99=3), gen(imm1_w1 imm1_w2 imm1_w3 imm1_w4 imm1_w5)
recode IM2_W* (77 98 99=3), gen(imm2_w1 imm2_w2 imm2_w3 imm2_w4 imm2_w5) 
recode IM3_W* (77 98 99=3), gen(imm3_w1 imm3_w2 imm3_w3 imm3_w4 imm3_w5) 
egen immiall_w1=rowmean(imm1_w1 imm2_w1 imm3_w1) 
egen immiall_w2=rowmean(imm1_w2 imm2_w2 imm3_w2)
egen immiall_w3=rowmean(imm1_w3 imm2_w3 imm3_w3)
egen immiall_w4=rowmean(imm1_w4 imm2_w4 imm3_w4)
egen immiall_w5=rowmean(imm1_w5 imm2_w5 imm3_w5)
foreach x in immiall_w1 immiall_w2 immiall_w3 immiall_w4 immiall_w5 {
gen extr`x'=`x'
replace extr`x'=6-extr`x' if extr`x'<3
replace extr`x'=extr`x'-3 
}

* attitude extremity: health care lthcare extremity
recode HC1_W* (1=5) (5=1) (4=2) (2=4) (3=3) (77 98 99=3), gen(hcare_w1 hcare_w2 hcare_w3 hcare_w4 hcare_w5)
foreach x in hcare_w1 hcare_w2 hcare_w3 hcare_w4 hcare_w5 {
gen extr`x'=`x'
replace extr`x'=abs(3-`x')
}

* attitude extremity: social safety net 
recode TSN1_W* (1=5) (5=1) (4=2) (2=4) (3=3) (77 98 99=3), gen(spendmore_w1 spendmore_w2 spendmore_w3 spendmore_w4 spendmore_w5) 
foreach x in spendmore_w1 spendmore_w2 spendmore_w3 spendmore_w4 spendmore_w5 {
gen extr`x'=`x'
replace extr`x'=abs(3-`x')
}

recode TSN2_W* (77 98 99=3), gen(taxcut_w1 taxcut_w2 taxcut_w3 taxcut_w4 taxcut_w5) 
foreach x in taxcut_w1 taxcut_w2 taxcut_w3 taxcut_w4 taxcut_w5 {
gen extr`x'=`x'
replace extr`x'=abs(3-`x')
}

* attitude extremity: trade 
recode TR1_W* (1=5) (5=1) (4=2) (2=4) (3=3) (77 98 99=3), gen(trade_w1 trade_w2 trade_w3 trade_w4 trade_w5) 
foreach x in trade_w1 trade_w2 trade_w3 trade_w4 trade_w5 {
gen extr`x'=`x'
replace extr`x'=abs(3-`x')
}

* attitude extremity index 
egen attextremeindx_w1=rowmean(extrimmiall_w1 extrhcare_w1 extrspendmore_w1 extrtaxcut_w1 extrtrade_w1) 
egen attextremeindx_w2=rowmean(extrimmiall_w2 extrhcare_w2 extrspendmore_w2 extrtaxcut_w2 extrtrade_w2) 
egen attextremeindx_w3=rowmean(extrimmiall_w3 extrhcare_w3 extrspendmore_w3 extrtaxcut_w3 extrtrade_w3) 
egen attextremeindx_w4=rowmean(extrimmiall_w4 extrhcare_w4 extrspendmore_w4 extrtaxcut_w4 extrtrade_w4) 
egen attextremeindx_w5=rowmean(extrimmiall_w5 extrhcare_w5 extrspendmore_w5 extrtaxcut_w5 extrtrade_w5) 
sum attextremeindx_w* 

* pid strength 
recode PARTYID7_W1 partyid7_w2 PARTYID7_W3 P_PARTYID7_W4 P_PARTYID7_W5 (-1 98=4), gen(pid7_w1 pid7_w2 pid7_w3 pid7_w4 pid7_w5)
recode pid7_* (4=0) (3 5=1) (2 6=2) (1 7=3), gen(pidstren_w1 pidstren_w2 pidstren_w3 pidstren_w4 pidstren_w5)
sum pidstren_w* 

* ideology strength 
recode IDEO_W1 IDEO_W2 IDEO_W3 P_IDEO_W4 P_IDEO_W5 (1=1) (2=2) (3=3) (4=4) (5=5) (6=6) (7=7) (else=4), gen(ideo7_w1 ideo7_w2 ideo7_w3 ideo7_w4 ideo7_w5) 
recode ideo7* (4=0) (3 5=1) (2 6=2) (1 7=3), gen(ideostren_w1 ideostren_w2 ideostren_w3 ideostren_w4 ideostren_w5)
sum ideostren_w*

* party feeling thermometer 
recode THERM3_W* (777 998=50), gen(therm3_w1 therm3_w2 therm3_w3 therm3_w5)
recode THERM4_W* (777 998=50), gen(therm4_w1 therm4_w2 therm4_w3 therm4_w5)
gen thermreps_w1=.
gen thermdems_w1=.
gen thermreps_w2=.
gen thermdems_w2=.
gen thermreps_w3=.
gen thermdems_w3=.
gen thermreps_w5=.
gen thermdems_w5=.
replace thermreps_w1=therm3_w1 if RND_03_W1==0
replace thermreps_w1=therm4_w1 if RND_03_W1==1
replace thermdems_w1=therm3_w1 if RND_03_W1==1
replace thermdems_w1=therm4_w1 if RND_03_W1==0
replace thermreps_w2=therm3_w2 if RND_03_W2==0
replace thermreps_w2=therm4_w2 if RND_03_W2==1
replace thermdems_w2=therm3_w2 if RND_03_W2==1
replace thermdems_w2=therm4_w2 if RND_03_W2==0
replace thermreps_w3=therm3_w3 if RND_03_W3==0
replace thermreps_w3=therm4_w3 if RND_03_W3==1
replace thermdems_w3=therm3_w3 if RND_03_W3==1
replace thermdems_w3=therm4_w3 if RND_03_W3==0
replace thermreps_w5=therm3_w5 if RND_03==0
replace thermreps_w5=therm4_w5 if RND_03==1
replace thermdems_w5=therm3_w5 if RND_03==1
replace thermdems_w5=therm4_w5 if RND_03==0

* party affect intensity 
gen thermpartydiff_w1=abs(thermreps_w1-thermdems_w1)
gen thermpartydiff_w2=abs(thermreps_w2-thermdems_w2)
gen thermpartydiff_w3=abs(thermreps_w3-thermdems_w3)
gen thermpartydiff_w5=abs(thermreps_w5-thermdems_w5)
sum thermpartydiff_w*

* trump feeling thermometer 
recode THERMTRUMP* (777 998=50), gen(thermtrump_w1 thermtrump_w2 thermtrump_w3 thermtrump_w4 thermtrump_w5)

* trump feeling intensity 
foreach x in thermtrump_w1 thermtrump_w2 thermtrump_w3 thermtrump_w4 thermtrump_w5 {
gen extr`x'=. 
replace extr`x'=abs(50-`x') if `x' < 51
replace extr`x'=(`x'-50) if `x' > 50
}
sum extrthermtrump_w*

* perceived discrimination against R's sex  
recode DIS4_W* (1=5) (5=1) (4=2) (2=4) (3=3) (else=3), gen(diswomen_w1 diswomen_w2 diswomen_w3 diswomen_w4 diswomen_w5)
recode DIS5_W* (1=5) (5=1) (4=2) (2=4) (3=3) (else=3), gen(dismen_w1 dismen_w2 dismen_w3 dismen_w4 dismen_w5)

forvalues i = 1(1)5 {
gen disownsex_w`i'=. 
replace disownsex_w`i' = diswomen_w`i' if GENDER_W1==2
replace disownsex_w`i' = dismen_w`i' if GENDER_W1==1
}
sum disownsex_w*

* perceived discrimination against R's race/ethnicity
recode DIS1_W* (1=5) (5=1) (4=2) (2=4) (3=3) (else=3), gen(disblack_w1 disblack_w2 disblack_w3 disblack_w4 disblack_w5)
recode DIS2_W* (1=5) (5=1) (4=2) (2=4) (3=3) (else=3), gen(dishisp_w1 dishisp_w2 dishisp_w3 dishisp_w4 dishisp_w5)
recode DIS3_W* (1=5) (5=1) (4=2) (2=4) (3=3) (else=3), gen(diswhite_w1 diswhite_w2 diswhite_w3 diswhite_w4 diswhite_w5)

forvalues i = 1(1)5 {
gen disownrace_w`i'=. 
replace disownrace_w`i' = diswhite_w`i' if RACETHNICITY_W1==1 //white
replace disownrace_w`i' = disblack_w`i' if RACETHNICITY_W1==2 //black
replace disownrace_w`i' = dishisp_w`i' if RACETHNICITY_W1==4 //hispanic
}
sum disownrace_w*

reshape long soctrustindx_w perpindx_w persecon_w attextremeindx_w pidstren_w ideostren_w thermpartydiff_w extrthermtrump_w disownsex_w disownrace_w, i(CaseId) j(wave)

foreach x in soctrustindx_w perpindx_w persecon_w attextremeindx_w pidstren_w ideostren_w thermpartydiff_w extrthermtrump_w disownsex_w disownrace_w {
quietly summarize `x'
replace `x'=(`x'-r(min))/(r(max)-r(min)) 
}

